package cpp.ece480.compteam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class MySQL_AndroidInterface {
	
	MySQL_AndroidInterface(){
		
	}
	//Creates new row and returns the new ID
	//Values and Columns are to be input comma separated. Ex: "x1, x2, x3"
    public String MySQL_INSERT(String server,String db, String table, String columns, String values){
    	String insertString = "http://"+server+"/insertToDatabase.php";
    	ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("columns",columns));
        myNVP.add(new BasicNameValuePair("values",values));
        
    	return MySQL_httpPost(insertString,myNVP);
    }
    
    // Extra!
    public String MySQL_UPDATE2(String server, String db, String table, String column, String item, String theCustomer, String customerNum, String theTable, String tableNum){
    	String updateString = "http://"+server+"/updateToDatabase2.php";
        ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("column",column));
        myNVP.add(new BasicNameValuePair("item",item));
        myNVP.add(new BasicNameValuePair("theCustomer",theCustomer));
        myNVP.add(new BasicNameValuePair("customerNum",customerNum));
        myNVP.add(new BasicNameValuePair("theTable",theTable));
        myNVP.add(new BasicNameValuePair("tableNum",tableNum));
        
    	return MySQL_httpPost(updateString,myNVP);
    }
    
    //Replaces value of column with id
    //Should return ""
    public String MySQL_UPDATE(String server, String db, String table, String column, String value, String id){
    	String updateString = "http://"+server+"/updateToDatabase.php";
        ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("column",column));
        myNVP.add(new BasicNameValuePair("value",value));
        myNVP.add(new BasicNameValuePair("id",id));
        
    	return MySQL_httpPost(updateString,myNVP);
    }
    
    //Returns value of column with id
    public String MySQL_READ(String server, String db, String table, String column, String id){
    	String readString = "http://"+server+"/readFromDatabase.php";
    	ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("column",column));
        myNVP.add(new BasicNameValuePair("id",id));
    	
    	return MySQL_httpPost(readString, myNVP);
    }
    
    public String MySQL_READ2(String server, String db, String table, String column, String theCustomer, String customerNum, String theTable, String tableNum){
    	String readString = "http://"+server+"/readFromDatabase2.php";
    	ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("column",column));
        myNVP.add(new BasicNameValuePair("theCustomer",theCustomer));
        myNVP.add(new BasicNameValuePair("customerNum",customerNum));
        myNVP.add(new BasicNameValuePair("theTable",theTable));
        myNVP.add(new BasicNameValuePair("tableNum",tableNum));
    	
    	return MySQL_httpPost(readString, myNVP);
    }
    
  //Returns value of column with id
    public String MySQL_READ3(String server, String db, String table, String column, String theItem, String item){
    	String readString = "http://"+server+"/readFromDatabase3.php";
    	ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("column",column));
        myNVP.add(new BasicNameValuePair("theItem",theItem));
        myNVP.add(new BasicNameValuePair("item",item));
    	
    	return MySQL_httpPost(readString, myNVP);
    }
    
    //Deletes row of column with id
    //Should return ""
    public String MySQL_DELETE(String server, String db, String table, String id){
    	String deleteString = "http://"+server+"/deleteFromDatabase.php";
    	ArrayList<NameValuePair> myNVP = new ArrayList<NameValuePair>();
        myNVP.add(new BasicNameValuePair("db",db));
        myNVP.add(new BasicNameValuePair("table",table));
        myNVP.add(new BasicNameValuePair("id",id));
    	
    	return MySQL_httpPost(deleteString, myNVP);
    }
    
    public String MySQL_httpPost(String updateString, ArrayList<NameValuePair> myNVP){
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPost httppost = new HttpPost(updateString);
    	String out = null;
      	try {
			httppost.setEntity(new UrlEncodedFormEntity(myNVP));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			
			out= sb.toString();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString());//printStackTrace());
		}
		return out;	
    }
    
}
